<?php
/**
 * Shop_Model_Promotion
 * 
 * @version $Id$
 * @author $Author$
 */
class Shop_Model_Promotion extends Promotor_Model_Scaffold {

	protected $_dbTableClass = 'Shop_Model_DbTable_Promotion';
	
	protected $_cachedMethods = array(
		'findAllInCategory',
	);
	
	public function selectList() {
		$db = $this->getDbTable()
				   ->getAdapter();

		$select = new Zend_Db_Select($db);

		$select->from(array('p' => 'shop_promotion'), array('*', 'is_active' => new Zend_Db_Expr(' CURRENT_DATE < p.t_end')))
			   ->join(array('sp' => 'shop_product'),
			   		  'p.product_id = sp.id', array('name','alias'));

		return $select;
	}

	/**
	 * Generowanie ceny produktu 
	 * 
	 * @param float $price		- cena
	 * @param string $modifer	- modyfikator ceny
	 * @param float $value		- wartośc o jaka jest modyfikowana cena
	 * @return float
	 */
	public static function price($price, $modifer, $value) {
		if (strlen($modifer) && is_numeric($value))
		{
			switch ($modifer)
			{
				case '%':
					$price = $price - ($price * $value / 100); break;

				case '+':
					$price += $value; break;

				case '-':
					$price -= $value; break;
			}
		}
		
		return number_format($price, 2);
	}
	
	/**
	 * @param Zend_Db_Table_Row_Abstract|array|integer $category
	 * @param integer $page
	 * @param integer $onPage
	 * @return array
	 */
	public function findAllInCategory($category = null,  $page = null, $onPage = null) {
		if (is_numeric($category)) {
			$category = (int) $category;
		} elseif ($category instanceof Zend_Db_Table_Row_Abstract) {
			$category = $category->id;
		} elseif (is_array($category)) {
			$category = isset($category['id'])
				? $category['id']
				: (isset($category['category_id']) ? $category['category_id'] : null);
		}

		$table = $this->getDbTable();
		$db = $table->getAdapter();

		$select = new Zend_Db_Select($db);
		$select->from(array('shc' => 'shop_has_category'), array())
			   ->joinInner(
			   		array('sp' => 'shop_product'),
			   		'shc.product_id = sp.id',
			   		array('*', 'promotion' => new Zend_Db_Expr('CURRENT_DATE BETWEEN spp.t_start AND spp.t_end')))
			   	
			   	->joinInner(
			   		array('spp' => 'shop_promotion'),
			   		'spp.product_id = sp.id AND CURRENT_DATE BETWEEN spp.t_start AND spp.t_end',
			   		array('modifer','value'))

			   	->joinLeft(
			   		array('si' => 'shop_image'),
			   		'si.id = sp.image_id',
			   		array('image_name' => 'name','image'))

			   	->where('sp.publicated = 1')
			   	->order('shc.idx ASC');
			   	
		if (null !== $category) {
			$select->where('shc.category_id = ?', $category, Zend_Db::INT_TYPE);
		}

		if (is_numeric($page) && is_numeric($onPage)) {
			$select->limitPage($page, $onPage);
		} elseif (is_numeric($onPage)) {
			$select->limit($onPage);
		} 

		try {
			$stmt = $select->query();
			$rowset = $stmt->fetchAll();
			$this->_setStatus(self::SUCCESS);
			return array($rowset, $select);
		} catch(Zend_Date_Exception $e) {
			$this->_addException($e);
			$this->_setStatus(self::FAILURE);
		}
	}
}